from matplotlib import pyplot as plt
import numpy as np
import torch

fig = plt.figure()
x = np.arange(-4, 4, 0.025)
plt.plot(x,x**2)
plt.title("y = x^2")
def f(x):
    return x**2
def h(x):
    return 2*x
# 学习率
η = 0.01
# 前一轮动量的缩放
α = 0.9
# 初始动量
v = 0
x = 4
iters = 0
X = []
Y = []
while iters<8000:
    iters+=1
    X.append(x)
    Y.append(f(x))
    v = α*v - η*h(x)
    x = x + v
    print(iters,x)
plt.plot(X,Y)
plt.show()

params = {

}
torch.optim.SGD(params, lr=0.01, momentum=0, dampening=0,
                 weight_decay=0, nesterov=False)